package com.dxd.onlineshop.service.impl;

import com.dxd.onlineshop.mapper.UserFeedbackMapper;
import com.dxd.onlineshop.pojo.UserFeedback;
import com.dxd.onlineshop.service.UserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserFeedbackServiceImpl implements UserFeedbackService {

    private final UserFeedbackMapper userFeedbackMapper;

    @Autowired
    public UserFeedbackServiceImpl(UserFeedbackMapper userFeedbackMapper) {
        this.userFeedbackMapper = userFeedbackMapper;
    }

    @Override
    @Transactional
    public UserFeedback createFeedback(UserFeedback feedback) {
        feedback.setCreatedAt(LocalDateTime.now());
        userFeedbackMapper.insert(feedback);
        return feedback;
    }

    @Override
    public UserFeedback getFeedbackById(Long id) {
        return userFeedbackMapper.selectById(id);
    }

    @Override
    public List<UserFeedback> getFeedbackByUserId(Integer userId) {
        return userFeedbackMapper.selectByUserId(userId);
    }

    @Override
    public List<UserFeedback> getAllFeedback() {
        return userFeedbackMapper.selectAll();
    }

    @Override
    public List<UserFeedback> getFeedbackByType(UserFeedback.FeedbackType type) {
        return userFeedbackMapper.selectByType(type);
    }

    @Override
    @Transactional
    public UserFeedback updateFeedback(UserFeedback feedback) {
        userFeedbackMapper.update(feedback);
        return feedback;
    }

    @Override
    @Transactional
    public void deleteFeedback(Long id) {
        userFeedbackMapper.deleteById(id);
    }


    @Override
    public List<UserFeedback> searchFeedbackByContentAndUserId(String keyword, Integer userId) {
        return userFeedbackMapper.searchByContentAndUserId(keyword, userId);
    }
}